const router = require('koa-router')()
const sqlQuery = require('../db')
const getRandomColor = require('../util/randomColor')
router.prefix('/index')

router.get('/findSomeVideos', async (ctx, next) => {
    try {
        let { currentPage, limit } = ctx.query;
        currentPage = parseInt(currentPage);
        limit = parseInt(limit)
        let startindex = (currentPage - 1) * limit;
        let sql = `SELECT hv.id,hv.title,hv.url,hv.navigator_url,hv.state,hc.name class_name,hv.user_id,t.image,hv.click
        FROM happy_video hv JOIN happy_class hc join texticon  t
        WHERE hv.class_id=hc.id  and hv.h_image =t.image_id  and hv.state=1 and hc.state=1  LIMIT ?,?`;
        let result = await sqlQuery(sql, [startindex, limit]);
        await Promise.all(result.map(async item => {
            let res = await sqlQuery('select count(*) count  from praise where video_id =?', [item.id])
            item.h_praise = res[0].count
        }));
        ctx.body = {
            status: 200,
            message: result
        }
    } catch (error) {
        next(error)
    }
})
router.get('/findSomeVideo', async (ctx, next) => {
    try {
        let { id } = ctx.query;
        let sql = `SELECT hv.id,hv.title,hv.url,hv.navigator_url,hv.state,hc.name class_name,hv.user_id,t.image,hv.click
        FROM happy_video hv JOIN happy_class hc join texticon  t
        WHERE hv.class_id=hc.id  and hv.h_image =t.image_id  and hv.state=1 and hc.state=1  and hv.id=? `;
        let result = await sqlQuery(sql, [id]);
        ctx.body = {
            status: 200,
            message: result
        }
    } catch (error) {
        next(error)
    }
})

module.exports = router
